{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "extra-thriller",
   "metadata": {},
   "source": [
    "## 使用Xgboost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "southwest-outside",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[17:24:55] WARNING: /Users/travis/build/dmlc/xgboost/src/learner.cc:1061: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "错误类为0.265625\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['2.model']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import pickle\n",
    "import xgboost as xgb\n",
    "from sklearn.model_selection import train_test_split\n",
    "import joblib\n",
    "\n",
    "# 用pandas读入数据\n",
    "data = pd.read_csv('data/Pima-Indians-Diabetes.csv')\n",
    "\n",
    "# 做数据切分\n",
    "train, test = train_test_split(data)\n",
    "\n",
    "# 取出特征X和目标y的部分\n",
    "feature_columns = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age']\n",
    "target_column = 'Outcome'\n",
    "train_X = train[feature_columns].values\n",
    "train_y = train[target_column].values\n",
    "test_X = test[feature_columns].values\n",
    "test_y = test[target_column].values\n",
    "\n",
    "# 初始化模型\n",
    "xgb_classifier = xgb.XGBClassifier(n_estimators=20,\\\n",
    "                                   max_depth=4, \\\n",
    "                                   learning_rate=0.1, \\\n",
    "                                   subsample=0.7, \\\n",
    "                                   colsample_bytree=0.7)\n",
    "\n",
    "# 拟合模型\n",
    "xgb_classifier.fit(train_X, train_y)\n",
    "# 使用模型预测\n",
    "preds = xgb_classifier.predict(test_X)\n",
    "# 判断准确率\n",
    "print ('错误类为%f' %((preds!=test_y).sum()/float(test_y.shape[0])))\n",
    "# 模型存储\n",
    "joblib.dump(xgb_classifier, '2.model')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "democratic-airline",
   "metadata": {},
   "source": [
    "## early-stopping 早停"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "veterinary-secret",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0]\tvalidation_0-auc:0.99950\n",
      "[1]\tvalidation_0-auc:0.99975\n",
      "[2]\tvalidation_0-auc:0.99975\n",
      "[3]\tvalidation_0-auc:0.99975\n",
      "[4]\tvalidation_0-auc:0.99975\n",
      "[5]\tvalidation_0-auc:0.99975\n",
      "[6]\tvalidation_0-auc:1.00000\n",
      "[7]\tvalidation_0-auc:1.00000\n",
      "[8]\tvalidation_0-auc:1.00000\n",
      "[9]\tvalidation_0-auc:1.00000\n",
      "[10]\tvalidation_0-auc:1.00000\n",
      "[11]\tvalidation_0-auc:1.00000\n",
      "[12]\tvalidation_0-auc:1.00000\n",
      "[13]\tvalidation_0-auc:1.00000\n",
      "[14]\tvalidation_0-auc:1.00000\n",
      "[15]\tvalidation_0-auc:1.00000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,\n",
       "              colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,\n",
       "              importance_type='gain', interaction_constraints='',\n",
       "              learning_rate=0.300000012, max_delta_step=0, max_depth=6,\n",
       "              min_child_weight=1, missing=nan, monotone_constraints='()',\n",
       "              n_estimators=100, n_jobs=12, num_parallel_tree=1, random_state=0,\n",
       "              reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=1,\n",
       "              tree_method='exact', validate_parameters=1, verbosity=None)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import xgboost as xgb\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn import datasets\n",
    "\n",
    "digits = datasets.load_digits(n_class=2)\n",
    "X = digits['data']\n",
    "y = digits['target']\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)\n",
    "clf = xgb.XGBClassifier()\n",
    "clf.fit(X_train, y_train, early_stopping_rounds=10, eval_metric=\"auc\",\n",
    "        eval_set=[(X_test, y_test)])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "unique-button",
   "metadata": {},
   "source": [
    "## 使用Xgboost输出特征重要程度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "binding-surface",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[22:32:38] WARNING: /Users/travis/build/dmlc/xgboost/src/learner.cc:1061: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'multi:softprob' was changed from 'merror' to 'mlogloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>feature_names</th>\n",
       "      <th>feature_importances</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>petal_length</td>\n",
       "      <td>0.676586</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>petal_width</td>\n",
       "      <td>0.297366</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>sepal_width</td>\n",
       "      <td>0.016450</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>sepal_length</td>\n",
       "      <td>0.009598</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  feature_names  feature_importances\n",
       "2  petal_length             0.676586\n",
       "3   petal_width             0.297366\n",
       "1   sepal_width             0.016450\n",
       "0  sepal_length             0.009598"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import xgboost as xgb\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn import datasets\n",
    "iris = datasets.load_iris()\n",
    "y = iris['target']\n",
    "X = iris['data']\n",
    "xgb_model = xgb.XGBClassifier().fit(X,y)\n",
    "\n",
    "temp = pd.DataFrame()\n",
    "temp['feature_names'] = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']\n",
    "temp['feature_importances'] = xgb_model.feature_importances_\n",
    "temp = temp.sort_values('feature_importances',ascending = False)\n",
    "temp"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
